<template>
	<view class="my-settle-container">
		<!-- 全选 -->
		<label class="radio" @click="changeAllState">
			<radio color="#C00000" :checked="isFullCheck" /><text>全选</text>
		</label>

		<!-- 合计 -->
		<view class="amount-box">
			合计:<text class="amount">￥{{checkedGoodsAmount}}</text>
		</view>

		<!-- 结算按钮 -->
		<view class="btn-settle" @click="confirmPay">结算({{checkedCount}})</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from 'vuex'

	export default {
		data() {
			return {
				// 倒计时的秒数
				seconds: 3,
				// 定时器的 Id
				timer: null
			};
		},
		computed: {
			...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
			...mapGetters('m_user', ['addstr']),
			...mapState('m_user', ['token']),
			...mapState('m_cart', ['cart']),
			isFullCheck() {
				return this.total === this.checkedCount
			}
		},
		methods: {
			...mapMutations('m_cart', ['updateAllGoodsState']),
			...mapMutations('m_user', ['updateRedirectInfo']),
			changeAllState() {
				this.updateAllGoodsState(!this.isFullCheck)
			},
			// 用户点击了结算按钮
			settlement() {
				if (!this.checkedCount) return uni.$showMsg('请选择要结算的商品！')

				if (!this.addstr) return uni.$showMsg('请选择收货地址！')

				// if (!this.token) return uni.$showMsg('请先登录！')
				if (!this.token) return this.delayNavigate()

				// this.payOrder()
				this.postPay()
			},
			// 创建订单
			async postPay() {
				const orderInfo =
					this.cart.filter(x => x.goods_state).map(x => ({
						id: x.goods_id,
						num: x.goods_count,
					}))

				console.log(orderInfo);
				// 1.2 发起请求创建订单
				let data = await uni.$dapi.postPay(orderInfo)
				if (data === undefined) return;
				console.log("msg ---> pay  ", data);
				this.gotoDetail(data.payForm, data.orderId)
			},
			gotoDetail(msg, orderId) {
				uni.setStorageSync('orderMsg', msg)
				uni.setStorageSync('orderId', orderId)
				uni.navigateTo({
					url: '../../subpkg/order_detail/order_detail'
				})
			},



			// 创建订单
			async payOrder() {
				// 1. 创建订单
				// 1.1 组织订单的信息对象
				const orderInfo = {
					// order_price: this.checkedGoodsAmount,
					order_price: 0.01,
					consignee_addr: this.addstr,
					goods: this.cart.filter(x => x.goods_state).map(x => ({
						goods_id: x.goods_id,
						goods_number: x.goods_count,
						goods_price: x.goods_price
					}))
				}

				// 1.2 发起请求创建订单
				let data = await uni.$dapi.postCreateOrder(orderInfo)
				if (data === undefined) return;

				// 1.3 得到服务器响应的“订单编号”
				const orderNumber = data.order_number

				// 2. 订单预支付
				// 2.1 发起请求获取订单的支付信息
				const data2 = await uni.$dapi.postReqUnifiedOrder(orderNumber) // 2.2 预付订单生成失败
				if (data2 === undefined) return;
				// 2.3 得到订单支付相关的必要参数
				const payInfo = data2.pay

				// 3. 发起微信支付
				// 3.1 调用 uni.requestPayment() 发起微信支付
				const [err, succ] = await uni.requestPayment(payInfo)
				// 3.2 未完成支付
				if (err) return uni.$showMsg('订单未支付！')
				// 3.3 完成了支付，进一步查询支付的结果
				const data3 = await uni.$dapi.postCheckOrder(orderNumber)
				// 3.4 检测到订单未支付
				if (data3 === undefined) return;
				// 3.5 检测到订单支付完成
				uni.showToast({
					title: '订单支付完成！',
					icon: 'success'
				})
			},
			// 延时导航到 my 页面
			delayNavigate() {
				this.seconds = 3

				this.showTips(this.seconds)

				this.timer = setInterval(() => {
					this.seconds--

					if (this.seconds <= 0) {
						clearInterval(this.timer)

						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								this.updateRedirectInfo({
									openType: 'switchTab',
									from: '/pages/cart/cart'
								})
							}
						})


						return
					}

					this.showTips(this.seconds)
				}, 1000)
			},
			async confirmPay() {

				uni.showModal({
					title: "确认支付",
					content: "你确认支付吗？"
				}).then(async (res) => {
					if (res.confirm === true) {
						const orderId = uni.getStorageSync("orderId")
						if (orderId != "") {
							const res = await uni.$dapi.checkOrderAndClear(orderId)
							if (!res) {
								uni.$showMsg("你还有订单未支付，请前去支付")
							}
						}
						this.settlement()
					}
				})
			},


			// 展示倒计时的提示消息
			showTips(n) {
				uni.showToast({
					icon: 'none',
					title: '请登录后再结算！' + n + '秒之后自动跳转到登录页',
					mask: true,
					duration: 1500
				})
			}
		}
	}
</script>

<style lang="scss">
	.my-settle-container {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 50px;
		background-color: white;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 14px;
		padding-left: 5px;

		.radio {
			display: flex;
			align-items: center;
		}

		.amount-box {
			.amount {
				color: #C00000;
				font-weight: bold;
			}
		}

		.btn-settle {
			background-color: #C00000;
			height: 50px;
			color: white;
			line-height: 50px;
			padding: 0 10px;
			min-width: 100px;
			text-align: center;
		}
	}
</style>